package org.gdkm.el;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;

/**
 * 逻辑层类. </br>
 * </br>
 * 日期: 2012-10-13
 * <p>
 * 
 * @author Arylo<<b>arylo.open@gmail.com</b>>
 * @version 1.0
 */
public class LogicThread {
	/**
	 * 检查网络的秒数,当前值为{@value} 秒
	 */
	public static final int check_timeout = 10;
	/**
	 * 登录超时的秒数,当前值为{@value} 秒
	 */
	public static final int login_timeout = 10;
	private static Thread thread = null;
	private static ButtonListener listener = null;

	/**
	 * 构建一个LogicThread对象.
	 */
	public LogicThread() {
		listener = new ButtonListener();
		this.CheckSocket();
	}
	
	private void CheckSocket() {
	}
	
	private void CheckSocket(String ip) {
		
	}
	
	private void CheckSocket(int port) {
		
	}
	private void CheckSocket(String ip, int port) {
		final JLabel text = (JLabel) Id.getView("CheckSocket_text");
		final JButton bn1 = (JButton) Id.getView("CheckSocket_button");
		final JButton bn2 = (JButton) Id.getView("CheckSocket_more");
		IntentThread.changeCheck();
		text.setText("Checking...");
		bn1.setVisible(false);
		bn2.setVisible(false);
		thread = new Thread() {
			@Override
			public void run() {
				for (int t = 0; t < check_timeout; t++) {
					if (TranspThread.isConnected() == true) {
						text.setText("Connected!!");
						try {
							this.sleep(1000);
						} catch (Exception e) {
							e.printStackTrace();
						}
						UiThread.changeLogin();
						this.stop();
					}
					try {
						this.sleep(1000);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				text.setText("NO SERVER!!");
				bn1.setText("Close");
				bn1.setVisible(true);
				bn1.setActionCommand("close");
				bn1.addActionListener(listener);
				bn2.setText("More");
				bn2.setVisible(true);
				bn2.setActionCommand("more");
				bn2.addActionListener(listener);
				try {
					this.sleep(1000);
				} catch (Exception e) {
					e.printStackTrace();
				}
				this.stop();
			}

		};
		thread.start();
		IntentThread.reSocket(ip, port);
	}
}
/**
 * <br>按钮监听类. </br>
 * <br>主要用监听按钮的点击.</br>
 * 日期: 2012-10-13
 * <p>
 * 
 * @author Arylo<<b>arylo.open@gmail.com</b>>
 * @version 1.0
 */
class ButtonListener implements ActionListener {
	/**
	 * 构建一个ButtonListener 对象.
	 */
	public ButtonListener() {
	}

	/**
	 * 响应点击按钮的方法.
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		String str = arg0.getActionCommand();
		if(str.equals("close") == true) {
			System.exit(0);
			return;
		}
		if(str.endsWith("more") == true) {
			return;
		}
	}
}